package studychinese.fragment;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hschinese.pronunciation.R;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.qqtheme.framework.picker.OptionPicker;
import studychinese.activity.HomeActivity;
import studychinese.callback.DialogCallback;
import studychinese.config.AppUrl;
import studychinese.config.ConFigTag;
import studychinese.config.Url;
import studychinese.model.NativePlanBean;
import studychinese.model.RegisterBean;
import studychinese.model.UserBean;
import studychinese.model.WhyStudayCZFBean;
import studychinese.utils.LevelsUtil;
import studychinese.utils.ToastUtils;

/**
 * A simple {@link Fragment} subclass.
 */
public class NativeStudayPlanFragment extends BaseFragment {


    @Bind(R.id.tv_native_titile)
    TextView tvNativeTitile;
    @Bind(R.id.rl_native_plan_time)
    RelativeLayout rlNativePlanTime;
    @Bind(R.id.tv_native_plan_time_count)
    TextView tvNativePlanTimeCount;
    @Bind(R.id.tv_native_plan_time_encourage)
    TextView tvNativePlanTimeEncourage;
    @Bind(R.id.btn_native_plan_time)
    Button btnNativePlanTime;
    @Bind(R.id.rl_native_translant_bk)
    RelativeLayout rlNativeTranslantBk;

    public NativeStudayPlanFragment() {
        // Required empty public constructor
    }


    public static NativeStudayPlanFragment newInstance() {

        Bundle args = new Bundle();

        NativeStudayPlanFragment fragment = new NativeStudayPlanFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_native_studay_plan, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_native_plan_time)
    public void onViewClicked() {
        RegisterBean.DataBean infor = UserBean.getInfor();
        if(infor!=null){
            if(infor.getCountry_id().equals("")){
                ToastUtils.showShort("请选择你的国家");
            }else if(infor.getInterest().equals("")){
                ToastUtils.showShort("请选择你为什么学习汉语");
            }else {
                subData(infor);

            }
        }


    }

    private void subData(RegisterBean.DataBean infor) {
        OkGo.<String>post(AppUrl.getAppUrl(Url.REGISTER_USERPORTRAIT)).tag(this)
                .params("user_id",infor.getUser_id())
                .params("country_id",infor.getCountry_id())
                .params("lang",infor.getLang())
                .params("interest",infor.getInterest())
                .params("user_initlevel",infor.getUser_initlevel())
                .params("user_curlevel",infor.getUser_curlevel())
                .params("user_targetlevel",infor.getUser_targetlevel())
                .params("learn_time",infor.getLearn_time())
                .params("user_age",infor.getUser_age())
                .execute(new DialogCallback<String>(getActivity(),true,"正在提交") {
            @Override
            public void onSuccess(Response<String> response) {
                NativePlanBean nativePlanBean = JSON.parseObject(response.body(), NativePlanBean.class);
                if(nativePlanBean.getState().equals(ConFigTag.state)){
                    startActivity(new Intent(getActivity(),HomeActivity.class));
                }else {
                    ToastUtils.showShort(nativePlanBean.getMessage());

                }

            }
        });

    }

    public void init() {
        setData();
    }

    private void setData() {
        OkGo.<WhyStudayCZFBean>post(AppUrl.getAppUrl(Url.REGISTER_USERLEARNTIME)) .cacheMode(CacheMode.IF_NONE_CACHE_REQUEST)
                .execute(new DialogCallback<WhyStudayCZFBean>(getActivity(),true) {
            @Override
            public void onSuccess(Response<WhyStudayCZFBean> response) {
                WhyStudayCZFBean body = response.body();
                if(body.getState().equals(ConFigTag.state)){
                    List<WhyStudayCZFBean.DataBean> data = body.getData();
                    RegisterBean.DataBean infor = UserBean.getInfor();
                    if(data.size()>0){
                        String id = data.get(0).getId();
                        infor.setLearn_time(id);
                        UserBean.putUserBean(infor);
                    }
                    setView(data);
                }
            }
        });
    }

    private void setView(final List<WhyStudayCZFBean.DataBean> data) {
        final List<String> list = new ArrayList<>();

        list.clear();
        for (int i = 0; i < data.size(); i++) {
            list.add(data.get(i).getName());
        }

        OptionPicker picker = new OptionPicker(getActivity(), list);
       /* picker.setCanceledOnTouchOutside(false);
        picker.setDividerRatio(WheelView.DividerConfig.FILL);*/

        picker.setSelectedIndex(0);
        picker.setCycleDisable(true);
        picker.setOffset(2);
        picker.setDividerVisible(true);
        picker.setLineSpaceMultiplier(4);
        picker.setDividerColor(Color.WHITE);
        picker.setTextSize(18);
        picker.setLineColor(Color.WHITE);
        picker.setTextColor(Color.WHITE,Color.WHITE);
        picker.setCycleDisable(true);
        picker.setOnOptionPickListener(new OptionPicker.OnOptionPickListener(

        ) {
            @Override
            public void onOptionPicked(int index, String item) {
                // showToast();

            }
        });
        picker.setOnWheelListener(new OptionPicker.OnWheelListener() {
            @Override
            public void onWheeled(int index, String item) {
                RegisterBean.DataBean infor = UserBean.getInfor();
                String days = new LevelsUtil().getDays(infor.getUser_targetlevel(), infor.getUser_initlevel(), data.get(index).getTime());
                //真棒你！你只需要21天就能达到目标了哦
                tvNativePlanTimeCount.setText("你用 "+days+" 天就能达到目标了哦");
                tvNativePlanTimeEncourage.setText(data.get(index).getContent());
                String id = data.get(index).getId();
                infor.setLearn_time(id);
                UserBean.putUserBean(infor);
            }
        });
        //得到选择器视图，可内嵌到其他视图容器，不需要调用show方法
        View pickerContentView = picker.getContentView();
        rlNativePlanTime.addView(pickerContentView);
    }

}
